package com.example.friend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.example.api.RemoteJudgeService;
import com.example.api.domain.dto.JudgeSubmitDTO;
import com.example.api.domain.vo.UserQuestionResultVO;
import com.example.common.core.domain.R;
import com.example.common.core.domain.ResultCode;
import com.example.friend.domain.question.QuestionCase;
import com.example.friend.domain.question.question.Question;
import com.example.friend.domain.user.dto.UserSubmitDTO;
import com.example.friend.mapper.question.QuestionMapper;
import com.example.friend.service.IUserQuestionService;
import com.example.security.exception.ServiceException;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class IUserQuestionServiceImpl implements IUserQuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    private RestTemplate restTemplate = new RestTemplate();

    @Override
    public R<UserQuestionResultVO> submit(UserSubmitDTO submitDTO) {
        Integer programType = submitDTO.getProgramType();
        if (programType == null || programType != 1) {
            throw new ServiceException(ResultCode.NON_SUPPORT_LANGUAGE);
        }
        JudgeSubmitDTO judgeSubmitDTO = createJudgeSubmitDTO(submitDTO);
        log.info(JSONUtil.toJsonStr(judgeSubmitDTO));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JudgeSubmitDTO> requestEntity = new HttpEntity<>(judgeSubmitDTO, headers);
        String url = "http://127.0.0.1:9998/judge/doJudgeJavaCode";  // 服务端接口完整 URL
        R<UserQuestionResultVO> response = restTemplate.postForObject(url, requestEntity, R.class);
        return response;
    }

    private JudgeSubmitDTO createJudgeSubmitDTO(UserSubmitDTO submitDTO) {
        JudgeSubmitDTO judgeSubmitDTO = new JudgeSubmitDTO();
        Long questionId = submitDTO.getQuestionId();
        Question question = questionMapper.selectById(questionId);
        BeanUtil.copyProperties(question, judgeSubmitDTO);
        judgeSubmitDTO.setExamId(submitDTO.getExamId());
        judgeSubmitDTO.setProgramType(submitDTO.getProgramType());
        judgeSubmitDTO.setUserCode(codeConnect(submitDTO.getUserCode(), question.getMainFunc()));
        List<QuestionCase> questionCases = JSONUtil.toList(question.getQuestionCase(), QuestionCase.class);
        List<String> inputList = questionCases.stream().map(QuestionCase::getInput).collect(Collectors.toList());
        List<String> outputList = questionCases.stream().map(QuestionCase::getOutput).collect(Collectors.toList());
        judgeSubmitDTO.setInputList(inputList);
        judgeSubmitDTO.setOutputList(outputList);
        return judgeSubmitDTO;
    }

    private String codeConnect(String userCode, String mainFunc) {
        String targetCharacter = "}";
        int targetLastIndex = userCode.lastIndexOf(targetCharacter);
        if (targetLastIndex != -1) {
            return userCode.substring(0, targetLastIndex) + "\n" + mainFunc + "\n" + userCode.substring(targetLastIndex);
        }
        throw new ServiceException(ResultCode.FAILED);
    }
}
